package assignment2;
//Author Mark Mckillion, Date Completed 09/12/2011

import java.awt.*;
import java.awt.geom.*;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class StackPanel extends JPanel
{   /*The purpose of this class is to update jPanel1 in the StackFrame class.
     *I will override paint component to display the current status of the stack
     *and will repaint it each time a change is by the action listeners.*/
    
    //Create an integer which will be set to equal values in the QueueFrame.
    private int count;
    
    //Create an instance of the queue which will be set to equal the queue in QueueFrame.
    @SuppressWarnings("rawtypes")
	private ArrayStack myStack;

    public void setCount(int value)
    {   //This set method allows the program to access the count value in StackFrame.
        count = value;
    }

    @SuppressWarnings("rawtypes")
	public ArrayStack setStack(ArrayStack stack)
    {   //This set method allows the program to access the myStack object in StackFrame.
        myStack = stack;
        return myStack;
    }

    @Override
    public void paintComponent(Graphics g)
    {   //Override paint component to design my JPanel
        //Call too the super class.
        super.paintComponent(g);
        
        //Create instances of GUI design packages.
        Graphics2D g2 = (Graphics2D)g;
        BasicStroke pen = new BasicStroke(1F);
        
        //Set the colour and thickness of the lines.
        g2.setColor(Color.BLACK);
        g2.setStroke(pen);
        
        //Create the empty rectangles.
        g2.draw(new Rectangle2D.Double(140,40,100,30));
        g2.draw(new Rectangle2D.Double(140,80,100,30));
        g2.draw(new Rectangle2D.Double(140,120,100,30));
        g2.draw(new Rectangle2D.Double(140,160,100,30));
        g2.draw(new Rectangle2D.Double(140,200,100,30));

        //Create the empty circles.
        g2.draw(new Ellipse2D.Float(300,40,50,30));
        g2.draw(new Ellipse2D.Float(300,80,50,30));
        g2.draw(new Ellipse2D.Float(300,120,50,30));
        g2.draw(new Ellipse2D.Float(300,160,50,30));
        g2.draw(new Ellipse2D.Float(300,200,50,30));
        
        //Draw the memory values into the circles as these are static.
        g2.drawString(String.valueOf(4),322,58);
        g2.drawString(String.valueOf(3),322,98);
        g2.drawString(String.valueOf(2),322,138);
        g2.drawString(String.valueOf(1),322,178);
        g2.drawString(String.valueOf(0),322,218);
        
        //Create an integer for the starting x coordinates.
        int xCoord = 215;
        
        for(int i = 0; i < count; i++)
        {   /*this for loop draws the string value at the top of the stack into 
             *the appropriate retangle by altering the xCoord integer*/
            g2.drawString(String.valueOf(myStack.toString(i)),145,xCoord-(i*40));
        }

        if(count >= 0)
        {   /*this if statement draws the top marker in the appropriate place
            by altering the xCoord integer.*/
            g2.drawString("top",120,xCoord-(count*40));
        }
    }
}